import java.io.*;
import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-09-23
 * Time: 14:33
 */
public class Main {
    //    public static void main(String[] args) {
//        StringBuilder s = new StringBuilder();
//        s.append(1234);
//        System.out.println(s.toString());
//        s.delete(0,s.length()-1);
//        System.out.println(s.toString());
//    }
    public static void main1465(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int[] nums = new int[6];
            for (int j = 0; j < 6; j++) {
                nums[j] = in.nextInt();
            }
            Arrays.sort(nums);
            boolean flag = false;
            for (int j = 0; j < 4; j++) {
                if (nums[j] + nums[j + 1] > nums[j + 2] &&
                        nums[(j + 3) % 6] + nums[(j + 4) % 6] > nums[(j + 5) % 6]) {
                    System.out.println("Yes");
                    flag = true;
                    break;
                } else if (nums[j] + nums[j + 2] > nums[(j + 3) % 6] &&
                        nums[j + 1] + nums[(j + 4) % 6] > nums[(j + 5) % 6]) {
                    System.out.println("Yes");
                    flag = true;
                    break;
                } else if (nums[j] + nums[(j + 3) % 6] > nums[(j + 4) % 6] &&
                        nums[j + 1] + nums[j + 2] > nums[(j + 5) % 6]) {
                    System.out.println("Yes");
                    flag = true;
                    break;
                } else if (nums[j] + nums[(j + 4) % 6] > nums[(j + 5) % 6] &&
                        nums[j + 1] + nums[j + 2] > nums[(j + 3) % 6]) {
                    System.out.println("Yes");
                    flag = true;
                    break;
                }
            }
            if (!flag) System.out.println("No");
        }
    }

    public static void mainafdafdfdsa(String[] args) {
        int[] arr = {7,8,2,6,1,1,7,2,4,3,4,7,5,6,8,2};
        Arrays.sort(arr);
        int ret = 1;
        int n = arr.length;
        int len = 1;
        for(int i = 0;i < n - 1;i++){
            if(arr[i] == arr[i + 1])continue;
            if(arr[i] + 1 == arr[i + 1]){
                len++;
            }else{
                ret = Math.max(ret,len);
                len = 1;
            }
        }
        ret = Math.max(ret,len);
        len = 1;
        System.out.println(ret);
    }

    public static void main12434(String[] args) {
        System.out.println(1234);
        System.out.println(Integer.reverse(1234));
    }
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static void main132(String[] args) throws IOException{
        int n = in.nextInt();
        int m = in.nextInt();
        int a = in.nextInt();
        int b = in.nextInt();
        int ret = 0;
            for(int i = 0;2 * i <= n && i <= m;i++){
            int j = 0;
            for(;j + 1 <= n - 2 * i && (j + 1) * 2 <= m - i;j++);
            ret = Math.max(i * a + j * b,ret);
        }
        out.println(ret);
        out.close();
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int[] nums = new int[a];
        long sum = 0;
        for(int i = 0;i < a;i++){
            nums[i] = in.nextInt();
        }
        int count = 0;
        int left = 0,right = 0;
        Arrays.sort(nums);
        int i = 0;
        int ret = 0;
        while(right < a){
            if(Math.abs(nums[right] - i) <= b)right++;
            else{
                if(Math.abs(nums[left] - (i + 1)) <= b)i++;
                else{
                    ret = Math.max(ret,right - left);
                    left++;
                }
            }
        }
        System.out.println(ret);
    }
}
class Read{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException {
        while(!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
}
